<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';

$order_id = intval(($_GET['order_id'])?$_GET['order_id']:$_POST['order_id']);
if (!$order_id) redirect("orders.php");

	
# Ако запазваме	
if ($_POST['is_submitted']) {
    $error_message = '';
        
	# OK
    if(!$error_message) {
				
		# Update the order status
		$sql = 'UPDATE orders
				SET
					order_status_id = '.intval($_POST['order_status_id']).'
				WHERE
					order_id = '.$order_id.'
				';
		query($sql);			
      	
		
		# Изпращане на е-мейл до потребителя
		
		
		///////////////
		
		$_SESSION['message'] = 'Order Status was updated and an e-mail is sent to user!';
		redirect("order_edit.php?order_id=".$order_id);
	  
    } else { //Има грешка
		
		$row_order =& $_POST;
		
	}
	
} else { //Първоначално зареждане
	
	$sql = 'SELECT
				o.start_date,
  				o.duration,
  				o.tuition,
				o.registration_fee,
				o.housing,
				o.order_status_id,
				o.creation_datetime AS application_datetime,
				u.*,
				s.name AS school_name,
				s.logo,
				c.name AS course_name,
				cou.name AS school_country,
				cou.country_id AS school_country_id,
				ci.name AS school_city
			FROM
				orders AS o,
				schools AS s,
				courses AS c,
				countries AS cou,
				cities AS ci,
				users AS u
			WHERE
				o.user_id = u.user_id
			AND
				o.school_id = s.school_id
			AND
				o.course_id = c.course_id
			AND
				s.city_id = ci.city_id
			AND
				cou.country_id = ci.country_id
			AND
				order_id = '.$order_id.'
		';
	$row_order = mysql_fetch_assoc(query($sql));
		
}


head_admin("Order Edit");
?>
		
		<?php if ($error_message) { ?>
		  <div class="message red">
			<h4>Error!</h4>
			<?=$error_message?>
		  </div>
		<?php } ?>
		
		<?php if ($_SESSION['message']) { ?>
        <div class="message green">
            <?=$_SESSION['message']?>
        </div>
        <?php unset($_SESSION['message']); } ?>
		
            <div class="topbuttons">
            	<a href="orders.php">Back to orders</a>
            </div>
            
            <h3>Order <?=number_pad($order_id,5)?> from <?=$row_order['application_datetime']?></h3>
            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="order_id" value="<?=$order_id?>" />
            
			<dl class="clearfix">
            
            <b>Current status</b>
            
            <select class="inputbox" name="order_status_id" style="width:170px">
			<?php
            $sql = "SELECT
                order_status_id,
                name
            FROM
                order_status
              ";
            $result = query($sql);
            while ($row_status = mysql_fetch_object($result)) {	  
            ?>
            
            <option value="<?=$row_status->order_status_id?>" <?=(($row_status->order_status_id==$row_order['order_status_id'])?'selected':'')?> ><?=$row_status->name?></option>
            
            <?php
            } 
            ?>	
            </select>
            
            <input style="float:right" type="submit" id="submit" value="Update" />
			
            </dl>
			</form>
            
            
            <h2>Course Information</h2><br>
                        
            <img src="<?=HTML_BASE?>/uploads/logos/<?=$row_order['logo']?>" align="right" alt="<?=$row_order['name']?>" />
            
            School: <b><?=$row_order['school_name']?></b><br>
            Location: <?=$row_order['school_city']?>, <?=$row_order['school_country']?><br><br>
            Course: <b><?=$row_order['course_name']?></b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            Tuition: <b>$<?=number_format($row_order['tuition'],2)?></b><br> 
            Start Date: <b><?=$row_order['start_date']?></b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            Duration: <b><?=$row_order['duration']?> weeks</b><br><br>
            
            
            <?php if ($row_order['housing']==1) { ?>
            <b>Information about school housing is requested.</b>
            <?php } ?>
            
            <br><br>
            <h2>Applicant Information</h2><br>
            
            Fisrt Name, Last Name:<br>
            <h3><?=$row_order['first_name']?> <?=$row_order['last_name']?></h3>
            
            <?=($row_order['married']==1)?"Married":"Single"?> <?=($row_order['gender']=='m')?"Male":"Female"?> from
            
            <?php
			$sql = "SELECT
						name
					FROM
						countries
					WHERE
						country_id = ".$row_order['hometown_country_id']."
					  ";
			$res_country = mysql_fetch_assoc(query($sql));
			
			echo $res_country['name'];
			?>
            <br>
            Date Of Birth: <?=date('Y/m/d',strtotime($row_order['date_of_birth']))?>,
            Phone Number: <?=$row_order['phone']?>
            
            <br><br>
            
            <h2>Contact Information</h2><br>
            
            <b>Street Address</b> <?=$row_order['street_address']?><br>
            
            <b>City</b> <?=$row_order['city']?><br>
            
            <b>State</b> <?=$row_order['state']?><br>
            
            <b>Zip Code</b> <?=$row_order['zipcode']?><br>
            
            <b>Country</b>
            <?php
			$sql = "SELECT
						name
					FROM
						countries
					WHERE
						country_id = ".$row_order['country_id']."
					  ";
			$res_country = mysql_fetch_assoc(query($sql));
			
			echo $res_country['name'];
			?><br><br>
            
            <b>Emergency Contact</b> <?=$row_order['emergency_contact']?>
            
            <br><br>
            
            <h2>Educational Background</h2><br>
            
            <b>Have you graduated from high school?</b><br>
			<small>Enter School Name and Year</small><br>
            <?=$row_order['high_school_name']?>,
            <?=$row_order['high_school_date']?>
            
            <br><br>

            <b>Have you graduated from college or university? </b><br>
			<small>Enter School Name and Year</small><br>
            <?=$row_order['university_name']?>,
			<?=$row_order['university_date']?>
            
            <br><br>
            <b>English Level</b><br>
                <input type="checkbox" disabled <?=($row_order['english_level']==1)?"checked":""?> >
                Beginner
                <input type="checkbox" disabled <?=($row_order['english_level']==2)?"checked":""?> >
                Elementary
                <input type="checkbox" disabled <?=($row_order['english_level']==3)?"checked":""?> >
                Low Intermediate
                <input type="checkbox" disabled <?=($row_order['english_level']==4)?"checked":""?> >
                High Intermediate
                <input type="checkbox" disabled <?=($row_order['english_level']==5)?"checked":""?> >
                Advanced
                
            <br><br>
            
            <b>Have you taken the TOEFL within the past 2 years?</b><br>
            <small>Enter Test Date and Score</small><br>
			<?=$row_order['toefl']?>
            
            <br><br>

            <b>What are your plans after completing this course?</b><br>
                <input type="checkbox" disabled <?=($row_order['future_plans']==1)?"checked":""?> >
                Study at University or College
                <input type="checkbox" disabled <?=($row_order['future_plans']==2)?"checked":""?> >
                Return to my country
                <input type="checkbox" disabled <?=($row_order['future_plans']==3)?"checked":""?> >
                Undecided
                <input type="checkbox" disabled <?=($row_order['future_plans']==4)?"checked":""?> >
                Other
                        
            <br><br>
            
            <b>Who will financially support your study abroad?</b><br>
                <input type="checkbox" disabled <?=($row_order['finances']==1)?"checked":""?> >
                Self-funded
                <input type="checkbox" disabled <?=($row_order['finances']==2)?"checked":""?> >
                Family
                <input type="checkbox" disabled <?=($row_order['finances']==3)?"checked":""?> >
                Company
                <input type="checkbox" disabled <?=($row_order['finances']==4)?"checked":""?> >
                School
                <input type="checkbox" disabled <?=($row_order['finances']==5)?"checked":""?> >
                Scholarship
                <input type="checkbox" disabled <?=($row_order['finances']==6)?"checked":""?> >
                Other
            
            <br><br>
            
            <?php if ($row_order['school_country_id']==235) { // United States ?>
            
            <h2>Visa Information</h2><br>
            
            <table class="application" width="100%">
            <tr><td colspan="2" >
            
            <b>Are you currently in the U.S.?</b><br>
            <label>
            <input type="radio" disabled <?=($row_order['in_us']==1)?"checked":""?> >
            Yes</label>
            <label>
            <input type="radio" disabled <?=($row_order['in_us']==0)?"checked":""?> >
            No</label>
            
            </td></tr>
            
            <tr><td width="50%">
            <br>
            <b>Do you currently have a visa?</b><br>
              <label>
                <input type="radio" disabled <?=($row_order['have_visa']==1)?"checked":""?> >
                Yes</label>
              <label>
                <input type="radio" disabled <?=($row_order['have_visa']==0)?"checked":""?> >
                No</label>
            
            </td><td>
            
            <b>What is your visa / immigration status?</b><br>
            <?=$row_order['visa_type']?>
            
            </td></tr>
            
            <tr><td colspan="2">
            <b>Are you currently attending school in the U.S.?</b><br>
              <label>
                <input type="radio" disabled <?=($row_order['attending_school']==1)?"checked":""?> >
                Yes</label>
              <label>
                <input type="radio" disabled <?=($row_order['attending_school']==0)?"checked":""?> >
                No</label>
            
            </td></tr>
            
            <tr><td>
            <br>
            <b>Name of institution issued your I-20?</b><br>
            <?=$row_order['i20_institution']?>
            
            </td><td>
            <br>
            <b>SEVIS ID number of your current I-20?</b><br>
            <?=$row_order['i20_id']?>
            
            </td></tr>
            
            </table>
            <br><br>
            
            <?php } ?>
            
            
            
            <a class="button" href="order_print.php?order_id=<?=$order_id?>" target="_blank">Print</a>
            
            
            
<?php
	foot_admin(); 
?>